package com.anlogic.sdk.ui.tests.utils;

import static org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory.withPartName;


import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.ICoreRunnable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotPerspective;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
import org.junit.Assert;

public class WorkspaceUtils {
	public static void resetworkbench(SWTWorkbenchBot bot) {
		bot.resetWorkbench();

		for (SWTBotView view : bot.views(withPartName("Welcome"))) {
			view.close();
		}
		SWTBotPerspective perspective = bot.perspectiveById("org.eclipse.cdt.ui.CPerspective");
		perspective.activate();
		bot.shell().activate();
	}
	
	public static void cleanWorkspace( ) {
		// Delete created projects after we are done with them
				IWorkspace workspace = ResourcesPlugin.getWorkspace();
				IProject[] projects = workspace.getRoot().getProjects();
				ICoreRunnable runnable = new ICoreRunnable() {
					@Override
					public void run(IProgressMonitor monitor) throws CoreException {
						for (IProject project : projects) {
							project.delete(true, true, null);
						}
					}
				};
				try {
					workspace.run(runnable, workspace.getRoot(), IWorkspace.AVOID_UPDATE, new NullProgressMonitor());
				} catch (CoreException e) {
					Assert.fail("Test failed due to exception: " + e.getMessage());
				}
	}

}
